package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.login.EnableLoginAuth;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.AccountLockResultVo;
import com.atguigu.tingshu.vo.account.AccountLockVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.catalina.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account/userAccount")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserAccountApiController {

    @Autowired
    private UserAccountService userAccountService;

    @PostMapping("/saveUserAccount/{userId}")
    public Result saveAccount(@PathVariable Long userId) {
        userAccountService.saveUserAccount(userId);
        return Result.ok();
    }

    @EnableLoginAuth
    @GetMapping("/getAvailableAmount")
    public Result<BigDecimal> getAvailableAmount() {
        Long userId = AuthContextHolder.getUserId();
        if (userId == null) {
            return Result.ok(new BigDecimal(0));
        }
        UserAccount userAccount = this.userAccountService.getOne(new LambdaQueryWrapper<UserAccount>()
                .eq(UserAccount::getUserId, userId)
                .select(UserAccount::getAvailableAmount));
        if (userAccount == null) {
            return Result.ok(new BigDecimal(0));
        }
        return Result.ok(userAccount.getAvailableAmount());
    }

    //验证余额并锁定余额
    @PostMapping("/check/lock")
    public Result<AccountLockResultVo> checkLock(@RequestBody AccountLockVo accountLockVo) {
        AccountLockResultVo resultVo = this.userAccountService.checkLock(accountLockVo);
        return Result.ok(resultVo);
    }

    //扣减余额
    @PostMapping("/reduceAccount/{orderNo}")
    public Result reduceAccount(@PathVariable String orderNo) {
        this.userAccountService.reduceAccount(orderNo);
        return Result.ok();
    }

    @EnableLoginAuth
    @GetMapping("/findUserRechargePage/{page}/{limit}")
    public Result<Page<UserAccountDetail>> findUserRechargePage(@PathVariable Integer page, @PathVariable Integer limit) {
        Page<UserAccountDetail> pages = this.userAccountService.findUserMoneyRecord(page, limit, SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT);
        return Result.ok(pages);
    }

    @EnableLoginAuth
    @GetMapping("/findUserConsumePage/{page}/{limit}")
    public Result<Page<UserAccountDetail>> findUserConsumePage(@PathVariable Integer page, @PathVariable Integer limit) {
        Page<UserAccountDetail> pages = this.userAccountService.findUserMoneyRecord(page, limit, SystemConstant.ACCOUNT_TRADE_TYPE_MINUS);
        return Result.ok(pages);
    }


}

